﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_TonKho
{
    public class K_TonKho_ViewModel : ViewModel<Accounting.Library.K_TonKho>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);

        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;
        public event EventHandler TimeChanged;
        public event EventHandler CountChanged;
        #endregion

        #region Refresh Model
        public K_TonKho_ViewModel()
        {
            BeginRefresh("NewK_TonKho");
            
        }

        public K_TonKho_ViewModel(int Khoa)
        {
            BeginRefresh("GetK_TonKho", Khoa);
           
        }

        public K_TonKho_ViewModel(DateTime date)
        {
            BeginRefresh("GetK_TonKho", date.Month +";"+ date.Year);

        }

        public void NewK_TonKhoViewModel()
        {
            BeginRefresh("NewK_TonKho");
            
        }

        public void GetK_TonKhoViewModel(int Khoa)
        {
            BeginRefresh("GetK_TonKho", Khoa);
        }

       
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.K_TonKho_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<K_TonKho_ChiTietChild>>(K_TonKho_ChiTiets_AddedNew);
                Model.TimeChanged += new EventHandler(OnTimeChanged);
                Model.CountChanged += new EventHandler(OnCountChanged);
            }
        }

        public void SaveAndNew()
        {
            BeginSave();
            NewK_TonKhoViewModel();
        }

        #endregion

        public void OnTimeChanged(object sender, EventArgs e)
        {

            if (TimeChanged != null)
            {
                TimeChanged(this, new EventArgs());
            }

        }

        public void OnCountChanged(object sender, EventArgs e)
        {

            if (CountChanged != null)
            {
                CountChanged(this, new EventArgs());
            }

        }

        #region Add K_TonKho Details Item
        public void AddK_TonKho_ChiTietItem()
        {
            Model.K_TonKho_ChiTiets.AddNewItem(Model.Khoa);
        }

       

        void K_TonKho_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<K_TonKho_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("K_TonKho_ChiTiets"));
        }      

       

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

      
        #endregion

        #region Delete K_TonKho Details Item

        public void DeleteK_TonKho_ChiTietItem(int Index)
        {
            Model.K_TonKho_ChiTiets.DeleteItem(Index);
        }

        #endregion

       
    }
}
